package hhh.app.presenter.model.productlist;

import android.util.SparseArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import hhh.app.common.L;
import hhh.app.data.bean.Category;
import hhh.app.data.bean.Product;
import hhh.app.domain.interactor.GetCategories;
import hhh.app.domain.interactor.GetProducts;
import hhh.app.domain.interactor.ProductCase;
import hhh.app.presenter.di.PerActivity;
import hhh.app.presenter.ui.base.BasePresenter;
import rx.Subscriber;

/**
 * Created by hhh on 2016/10/28.
 */
@PerActivity
public class ProductListPresenter extends BasePresenter<ProductListContract.IProductListView> implements ProductListContract.IProductListPresenter {
//    private ProductListContract.IProductListView indexView;

    private final GetProducts getProductsCase;
    private final GetCategories getCategories;

    private List<Category> group=new ArrayList<Category>();
    private SparseArray<List<Category>> children=new SparseArray<List<Category>>();

    @Inject
    public ProductListPresenter(@Named("productUrl") ProductCase getProductListCase,@Named("categories") ProductCase getCategories) {
        if(getProductListCase instanceof GetProducts) {
            this.getProductsCase = (GetProducts)getProductListCase;
        }else{
            this.getProductsCase =null;
        }
        if(getCategories instanceof GetCategories){
            this.getCategories = (GetCategories)getCategories;
        }else{
            this.getCategories = null;
        }
    }

    @Override
    public void loadProducts(int page,int category,String type,String sort) {
        if(this.getProductsCase !=null) {

            this.getProductsCase.setWord(type).setStart(page).setCategory(category).setSort(sort).execute(new Subscriber<List<Product>>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    L.d(e.toString());
                }

                @Override
                public void onNext(List<Product> products) {
//                    if(products.size()!=0){
                        view.showProducts(products);
//                    }else{
//                        view.showLoading();
//                    }
                }
            });
        }
    }

    @Override
    public void openProductDetail(Product p) {

    }

    @Override
    public void loadCategories() {
        if(this.getCategories!=null){
            this.getCategories.execute(new Subscriber<List<Category>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    L.e(e.toString());
                }

                @Override
                public void onNext(List<Category> categories) {
                    if(categories!=null){
                        view.initCategories(buildGroupChildren(categories));
                    }
                }
            });
        }
    }

    private Map<Integer,List<Category>> buildGroupChildren(List<Category> categories){
        Map<Integer,List<Category>> map=new HashMap<Integer, List<Category>>();
        int count=categories.size();
        for(int i=0;i<count;i++){
            Category c=categories.get(i);
            if(map.get(c.getParent_id())==null){
                List<Category> list=new ArrayList<Category>();
                list.add(c);
                map.put(c.getParent_id(),list);
            }else{
                map.get(c.getParent_id()).add(c);
            }
        }
        return map;
    }

}
